package com.it.Controller;

import com.it.Service.FlightService;
import com.it.pojo.Flight;
import com.it.pojo.PageBean;
import com.it.pojo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;
@CrossOrigin(origins = "*") // 解决跨域问题，允许所有来源访问，实际中可配置具体域名
@RestController
public class FlightController {
    @Autowired
    private FlightService flightService;

    //直达航班的分页查询
    @GetMapping("/straightFlight")
    public Result FSList(String departCity, String arriveCity, Integer peoNumber,
                         @DateTimeFormat(pattern = "yyyy-MM-dd")LocalDate day,
                         @RequestParam(defaultValue = "1") Integer page,
                         @RequestParam(defaultValue = "10") Integer pageSize){
        if(day == null){
            return Result.error("请选择出发日期");
        }
        PageBean pageBean = flightService.FSList(departCity,arriveCity,peoNumber,day,page,pageSize);
        return Result.success(pageBean);
    }

    //中转航班的分页查询
    @GetMapping("/turnFlight")
    public Result FCList(String departCity, String arriveCity, Integer peoNumber,
                         @DateTimeFormat(pattern = "yyyy-MM-dd")LocalDate day,
                         @RequestParam(defaultValue = "1") Integer page,
                         @RequestParam(defaultValue = "10") Integer pageSize){
        PageBean pageBean = flightService.FCList(departCity,arriveCity,peoNumber,day,page,pageSize);
        return Result.success(pageBean);
    }

    @GetMapping("/city")
    public Result getTurnCityByName(@RequestParam List<String> aName){
        List<String> turnCity = flightService.getTurnCityByName(aName);
        return Result.success(turnCity);
    }

    @GetMapping("/flight")
    public Result getFlightById(String fNumber){
        Flight flight = flightService.getFlightById(fNumber);
        return Result.success(flight);
    }
}
